using System;
using Microsoft.SPOT;
using System.IO;

namespace TemperatureMicro
{
    public enum DeviceType { sensor = 0, regulator = 1 };

    class Configuration
    {
        private static String RegulatorConfigPath = @"\SD\RegulatorConfig.txt";
        private static String SensorConfigPath = @"\SD\SensorConfig.txt";

        public static bool SensorConfigFilePresent() { return File.Exists(SensorConfigPath); }
        public static bool RegulatorConfigFilePresent() { return File.Exists(RegulatorConfigPath); }

        public void SaveSensorId(int sensorId){
            var sensorString = "SensorId=" + sensorId;
            WriteFile(sensorString, SensorConfigPath);
        }

        public void SaveRegulatorId(int regulatorId)
        {
            var regulatorString = "RegulatorId=" + regulatorId;
            WriteFile(regulatorString, RegulatorConfigPath);
        }

        public string GetId(DeviceType device)
        {
            string filePath = "";
            switch (device)
            {
                case DeviceType.sensor:
                    filePath = SensorConfigPath;
                    break;
                case DeviceType.regulator:
                    filePath = RegulatorConfigPath;
                    break;
            }

            string sensorString = ReadFile(filePath);
            if (!(sensorString.IndexOf("false") >= 0))
            {
                string[] sensorSettings = sensorString.Split('=');
                return sensorSettings[1];
            }
            else
            {
                return "Failed";
            }
        }

        private string ReadFile(string filePath)
        {
            string returnString = "";
            try
            {
                if (File.Exists(filePath))
                {
                    Debug.Print("'" + filePath + "' available");
                    StreamReader myFile = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read));
                    returnString = myFile.ReadToEnd();
                    myFile.Close();
                }
                else
                {
                    Debug.Print("'" + filePath + "' is NOT available");
                    returnString = "false";
                }
                
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
                returnString = "false";
            }
            return returnString;
        }

        private void WriteFile(String configString, String configFile)
        {
            File.Delete(configFile);
            using (var fileStream = new FileStream(configFile, FileMode.OpenOrCreate, FileAccess.Write))
            {
                StreamWriter myFile = new StreamWriter(fileStream);
                myFile.Write(configString);
                Debug.Print("'" + configString + "' saved to file: " + configFile);
                myFile.Close();
            }
        }
    }
}
